package com.pencilisland.pay.app.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pencilisland.pay.api.bean.constant.WxpayConstant;
import com.pencilisland.pay.api.bean.wxpay.WxpayCallbackNotifyResp;
import com.pencilisland.pay.app.service.AppWxpayService;

/**
 * 微信支付相关处理
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/app/wxpay")
public class WxpayController {
	
	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	AppWxpayService wxpayService;
	

	/**
	 * 支付回调
	 */
	@RequestMapping(value = "/callback/notify")
	public ResponseEntity<WxpayCallbackNotifyResp> callbackNotify(@RequestBody String requestXml) {
		log.info("微信支付回调处理【{}】",requestXml);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(WxpayConstant.APPLICAIONT_XML_UTF8);

		WxpayCallbackNotifyResp resp = null;
		try {
			resp = wxpayService.callbackNotify(requestXml);
		} catch (Exception e) {
			e.printStackTrace();
			resp = new WxpayCallbackNotifyResp();
			resp.setReturnCode(WxpayConstant.FAIL);
			resp.setReturnMsg("处理异常");
		}

		return new ResponseEntity<WxpayCallbackNotifyResp>(resp, headers, HttpStatus.OK);
	}
}
